function POIMarker(marker_opts){
    this.opts = marker_opts;
    if(this.opts.saved == undefined)
        this.opts.saved = true;
    if(this.opts.isCurrentLoc == undefined)
        this.opts.isCurrentLoc = false;
    if(this.opts.isCurrentLoc)
        this.opts.icon = "/images/path_blue.png";
    if(!this.opts.saved)
        this.opts.icon = "/images/path_green.png";
    this.opts.draggable = true;
    this.marker = new google.maps.Marker(this.opts);
     
    google.maps.event.addListener(this.marker, 'click', function (event){
        var marker = this;
        if(this.isCurrentLoc){
            showInfoWindow(marker,marker.getTitle());
        }
        else if(this.saved && this.poi){
            $.post('/marker_info', {'id': this.poi.id},function(data){showInfoWindow(marker,$(data)[0])});
        }
        else{
            $.post('/marker_form', function(data){showInfoWindow(marker,$(data)[0])});
        }
        if(this.poi)
            update_comment(this.poi);
    });
}

function setMarkerHandler(marker,infowindow){
    $('.save_marker').submit(function(){
        var dataSent = {
                'title': $(this).find('#title').val(), 
                'desc1': $(this).find('#desc1').val(),
                'desc2': $(this).find('#desc2').val(),
                'desc3': $(this).find('#desc3').val(),
                'private': $(this).find('#private').attr('checked'),
                'lat': marker.getPosition().lat(),
                'lon': marker.getPosition().lng()};
        if(marker.poi)
            dataSent.id = marker.poi.id;
        $.post('/save_marker', dataSent , function(dataRecieved){saveMarkerInfo(marker,infowindow,dataRecieved)}, 'json');
        return false;
    }); 
    if(marker.poi){
        $('#edit_marker_'+marker.poi.id).click(function(event){
            editMarker(marker,infowindow);
            return false;
        });
        $('#delete_marker_'+marker.poi.id).click(function(event){
            deleteMarker(marker,infowindow);
            return false;
        });        
    }
}

function showInfoWindow(marker,newcontent){
    if(!marker.infoContent)
     marker.infoContent = newcontent;
    var infowindow = new google.maps.InfoWindow({'content': marker.infoContent});
    infowindow.open(marker.map,marker);  
    google.maps.event.addListener(infowindow, 'domready', function(){setMarkerHandler(marker,infowindow)});
    google.maps.event.addListener(infowindow, 'closeclick', function(){marker.infoContent = infowindow.getContent()});
}
function updateInfoWindow(marker,infowindow,newcontent){
    marker.infoContent = newcontent;
    infowindow.setContent(newcontent);
}

function saveMarkerInfo(marker,infowindow,data){
    if(data.error){
        alert("error saving POI: "+data.error);
    }
    else{
        var poi = data;
        marker.setIcon(undefined);
        updateInfoWindow(marker,infowindow,$(data.html)[0]);
        marker.setTitle(poi.title);
        infowindow.close();
        poi.lat /= 1000000.0;
        poi.lon /= 1000000.0;
        if(data.old){
            $('#marker_table_id_'+poi.id).html(
                "<td>"+poi.id+"</td>\n"+
                "<td>"+poi.attribution+"</td>\n"+
                "<td>"+poi.private+"</td>\n"+
                "<td>"+poi.title+"</td>\n"+
                "<td>"+poi.lat+","+poi.lon+"</td>\n"+
                "<td>"+poi.line2+"</td>\n"+
                "<td>"+poi.line3+"</td>\n"+
                "<td>"+poi.line4+"</td>\n"
            );
        }
        else{
            $('#marker_list').append(
            '<tr id="marker_table_id_'+poi.id+'">\n'+
                "<td>"+poi.id+"</td>\n"+
                "<td>"+poi.attribution+"</td>\n"+
                "<td>"+poi.private+"</td>\n"+
                "<td>"+poi.title+"</td>\n"+
                "<td>"+poi.lat+","+poi.lon+"</td>\n"+
                "<td>"+poi.line2+"</td>\n"+
                "<td>"+poi.line3+"</td>\n"+
                "<td>"+poi.line4+"</td>\n"+
            "</tr>");
            

        }
        update_comment(poi);
        marker.poi = poi;
        setMarkerHandler(marker,infowindow);
    }
}

function editMarker(marker,infowindow){
    if(marker.editing) return false;
    $.post('/marker_form', {'id': marker.poi.id}, function(data){
        marker.saved = false;
        marker.setIcon("/images/path_green.png");
        updateInfoWindow(marker,infowindow,$(data)[0]);
        marker.editing = false;
    });
    marker.editing = true;
    return false;   
}

function deleteMarker(marker,infowindow){
    if(marker.deleting) return false;
    $.post('/delete_marker', {'id': marker.poi.id}, function(data){
        if(data.error)
            alert("Error: "+data.error);
        else{
            infowindow.close();
            marker.setMap(null);
            $('#marker_table_id_'+marker.poi.id).remove();
            $('#current_marker_comments').empty();
        } 
        marker.deleting = false;
    }, 'json');
    marker.deleting = true;
    return false;
}

function update_comment(poi){
    $.get('/comments', {'poi_id':poi.id}, function(data){
       $('#current_marker_comments').empty();
       var res = $(data);
       var res2 = res.children('#comments_container');
       $('#current_marker_comments').append(res);
    });
}
